package com.tanhua.dubbo.api;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.mongo.UserLike;
import com.tanhua.model.mongo.Visitors;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class UserLikeApiImpl implements UserLikeApi{

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Boolean saveOrUpdate(Long userId, Long likeUserId, boolean isLike) {
        try {
            //1、查询数据
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            //2、如果不存在，保存
            if(userLike == null) {
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(likeUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(isLike);
                mongoTemplate.save(userLike);
            }else {
                //3、更新
                Update update = Update.update("isLike", isLike)
                        .set("updated",System.currentTimeMillis());
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }



    //查询当前用户的喜欢列表
    public List<UserLike> queryMyLikeList(Long userId, Integer page, Integer pagesize) {
        //构造查询条件

        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true))
                .skip((page-1)*pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));

        return mongoTemplate.find(query, UserLike.class);
    }

    //查询当前用户的粉丝列表
    public List<UserLike> queryMyFansList(Long userLikeId, Integer page, Integer pagesize) {
        Query query = Query.query(Criteria.where("likeUserId").is(userLikeId).and("isLike").is(true))
                .skip((page-1)*pagesize).limit(pagesize).with(Sort.by(Sort.Order.desc("created")));

        return mongoTemplate.find(query, UserLike.class);
    }

    //根据用户id和取消喜欢的用户id删除用户
    public Boolean deleteLike(Long userId, Long likeUserId) {
        try {
            Query query = Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(likeUserId));
            mongoTemplate.remove(query,UserLike.class);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //保存MongoDB中UserLike集合用户(有就修改,没有就添加)
    public Boolean saveOrUpdateFans(Long userId, Long fansUserId) {
        try {
            Query query =Query.query(Criteria.where("userId").is(userId).and("likeUserId").is(fansUserId));
            UserLike userLike = mongoTemplate.findOne(query, UserLike.class);
            if (userLike == null){
                //没有添加
                userLike = new UserLike();
                userLike.setUserId(userId);
                userLike.setLikeUserId(fansUserId);
                userLike.setCreated(System.currentTimeMillis());
                userLike.setUpdated(System.currentTimeMillis());
                userLike.setIsLike(true);
            }else {
                //有修改
                Update update = Update.update("isLike",true);
                mongoTemplate.updateFirst(query,update,UserLike.class);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 查询互相喜欢数量
     *
     * @param userId
     * @return
     */
    @Override
    public Integer eachOtherLikeUserCount(Long userId) {
        Integer count = 0;
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        Query query1 = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        List<UserLike> userLikes1 = mongoTemplate.find(query1, UserLike.class);
        if (CollUtil.isEmpty(userLikes) || CollUtil.isEmpty(userLikes1)) {
            return count;
        }
        for (UserLike userLike : userLikes) {
            for (UserLike like : userLikes1) {
                if (userLike.getLikeUserId() == like.getUserId()) {
                    count++;
                }
            }
        }
        return count;
    }

    /**
     * 查看喜欢数量
     *
     * @param userId
     * @return
     */
    @Override
    public Integer LikeUserCount(Long userId) {
        Query query = Query.query(Criteria.where("userId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query, UserLike.class);
        if (CollUtil.isEmpty(userLikes)) {
            return 0;
        }
        Integer size = userLikes.size();
        return size;
    }

    @Override
    public Integer fanCount(Long userId) {
        Query query1 = Query.query(Criteria.where("likeUserId").is(userId).and("isLike").is(true));
        List<UserLike> userLikes = mongoTemplate.find(query1, UserLike.class);
        return userLikes.size();
    }


}